package com.lute.model;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.oauth2.common.json.JSONException;
import org.springframework.security.oauth2.common.json.JSONObject;

import com.lute.utils.ServerErrorResponse;

public class UserEntitlement {
	
	public static EmployeeAccountingPeriod checkUserPrivilegeToEmployeeAccountingPeriod
							(String from,int sessionUserId, int userId, String role,JSONObject jsonRes) throws JSONException {
		
		EmployeeAccountingPeriod employeeAccountingPeriod  = null;
		
		if(sessionUserId == userId) {
			if(role.equals("employee")) {
				employeeAccountingPeriod = Employee.getAccountingPeriod("id_user", userId,
						ClientAccountingPeriodState.OPEN.toString(), from);
			} else if(role.equals("approver")) {
				employeeAccountingPeriod = Approver.getAccountingPeriod("id_user", userId,
						ClientAccountingPeriodState.OPEN.toString(), from);
			/* ClientAdmin staff done in other requests */
			} else if(role.equals("clientAdmin")) {
				jsonRes.put("result",ServerErrorResponse.MALFORMED_REQUEST.toString());
			} else {
				/* Administrator */
			}
		} else {
			if(role.equals("approver")) {
				boolean privilege = Approver.checkPrivilege("id_user", sessionUserId , userId);
				if(privilege) {
					employeeAccountingPeriod = Employee.getAccountingPeriod("id_user", userId,
							ClientAccountingPeriodState.OPEN.toString(), from);
				}else {
					jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
				}
			} else if(role.equals("employee")) {
				jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
			/* ClientAdmin case */
			} else if(role.equals("clientAdmin")) {
				boolean privilege = ClientAdmin.checkPrivilege("id_user", sessionUserId, userId);
				if(privilege) {
					/* check user's role */
					String userRole = User.checkUserRole(userId);
					if(userRole.equals("employee")) {
						employeeAccountingPeriod = Employee.getAccountingPeriod("id_user", userId,
								ClientAccountingPeriodState.OPEN.toString(), from);
					} else {
						employeeAccountingPeriod = Approver.getAccountingPeriod("id_user", userId,
								ClientAccountingPeriodState.OPEN.toString(), from);
					}
				}else {
					jsonRes.put("result", ServerErrorResponse.USER_NOT_ENTITLED.toString());
				}
			} else {
				/* Administrator */
			}
		}
		return employeeAccountingPeriod;
	}
	
	public static List<EmployeeAccountingPeriod> checkUserPrivilegeToEmployeeAccountingPeriods
					(String from,int sessionUserId, int[] userIds, String role,JSONObject jsonRes) throws JSONException {
		
		List<EmployeeAccountingPeriod> employeeAccountingPeriods  = new ArrayList<EmployeeAccountingPeriod>();
		EmployeeAccountingPeriod employeeAccPeriod = null;
		for(int i = 0; i < userIds.length; i++) {
			employeeAccPeriod = checkUserPrivilegeToEmployeeAccountingPeriod(from,sessionUserId,userIds[i],role,jsonRes);
			if(employeeAccPeriod != null) {
				employeeAccountingPeriods.add(employeeAccPeriod);
			} else {
				return null;
			}
		}
		return employeeAccountingPeriods;
	}
	
	public static ClientProfile checkClientAdminPrivilegeToClientProfile
									(int sessionUserId, int clientId) throws JSONException {
		ClientProfile clientProfile = ClientProfile.getClientProfileWithClientAdminsFromDB
											("client.id", clientId);
		List<ClientAdmin> clientAdmins = clientProfile.getClientAdmin();
		boolean hasPrivilege = false;
		for(ClientAdmin clientAdmin: clientAdmins) {
			if(clientAdmin.getId_user() == sessionUserId) {
				hasPrivilege = true;
			}
		}
		if(!hasPrivilege) {
			return null;
		}
		return clientProfile;
	}
	
	public static List<ClientProfile> checkClientAdminPrivilegeToClientProfiles
									(int sessionUserId, int[] clientIds) throws JSONException {
		
		List<ClientProfile> clientProfile = new ArrayList<ClientProfile>();
		for(int i = 0; i < clientIds.length; i++) {
			ClientProfile tmp = checkClientAdminPrivilegeToClientProfile(sessionUserId,clientIds[i]);
			if(tmp == null)return null;
			List<ClientAdmin> clientAdmins = tmp.getClientAdmin();
			boolean hasPrivilege = false;
			for(ClientAdmin clientAdmin: clientAdmins) {
				if(clientAdmin.getId_user() == sessionUserId) {
					hasPrivilege = true;
					clientProfile.add(tmp);
				}
			}
			if(!hasPrivilege) {
				return null;
			}
		}
		return clientProfile;	
	}
	
	public static ClientProfile checkEmployeePrivilegeToClient(String role, int approverId, int clientId) {
		EmployeeProfile employeeProfile = EmployeeProfile.getEmployeeProfileByEmployeeIdFromDB(role, approverId);
		if(employeeProfile.getClient().getId_client() == clientId) {
			return employeeProfile.getClient().getClientProfil();
		}
		return null;
	}
	
	public static boolean checkPrivilegeToAccountLock(String role, User user) {
		if(role.equals("admin")) {
			return true;
		} else {
			if(!(user.getRole().getName().equals("admin"))) {
				return true;
			}
		}
		return false;
	}
}
